package com.qigao.mall.channel.alipay;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelCodeEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author:luiz
 * @Date: 2018/5/28 11:16
 * @Descripton:支付宝条码支付
 * @Modify : 当面付产品介绍 https://docs.open.alipay.com/194
 * 扫码付接口文档：https://docs.open.alipay.com/api_1/alipay.trade.precreate
 **/
@Service
public class AliPaySaoMaPay extends AbstractAliPay implements ChannelCore {
    private static final Logger logger= LoggerFactory.getLogger(AliPaySaoMaPay.class);

    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.CREATE_ORDER.equals(channelActionEnum) &&
                PayProductEnum.ALIPAY_SAOMA.equals(payProductEnum) &&
                ChannelInfoEnum.ALIPAY.equals(channelInfoEnum)
                ;
    }

    @Override
    protected String getTradeMethod() {
        return "alipay.trade.precreate";
    }


    @Override
    public Map buildSelfReqParam(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        Map reqParam=new ConcurrentHashMap<>();
        JSONObject bizContent=new JSONObject();
        bizContent.put("out_trade_no", merChannelDto.getInnerSeqNo());
        bizContent.put("product_code","FACE_TO_FACE_PAYMENT");
        JSONObject reqContent= JSONObject.parseObject(channelReqParam.getReqContent());
        bizContent.put("subject",reqContent.getString("goodsName"));
        bizContent.put("total_amount",reqContent.getString("payAmt"));
        bizContent.put("body",reqContent.getString("goodsName"));
        reqParam.put("biz_content",bizContent.toString());
        return reqParam;
    }

    @Override
    protected ChannelRespParam buildSelfRespData(JSONObject respJson, MerChannelDto merChannelDto) {
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        String qrCode=respJson.getString("qr_code");
        JSONObject respJsonObj=new JSONObject();
        respJsonObj.put("qrCode",qrCode);
        channelRespParam.setRespContent(respJsonObj.toJSONString());
        return channelRespParam;
    }

}
